package com.wzx.common.pojo.resp;

import com.wzx.common.constants.StatusCode;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * @author wangzixian
 * @date 2022/1/21 - 9:32
 */
@ApiModel(description = "通用返回响应结果对象", value = "result")
@Data
public class Result<T> {
    @ApiModelProperty(notes = "响应结果的主要内容", required = true)
    private ResultData<T> data;
    @ApiModelProperty(notes = "响应结果的错误信息状态码,200表示请求成功", required = true)
    private Integer status;

    private static final Integer SUCCESS_CODE = 200;

    /**
     * 1.1，成功并不返回数据
     *
     * @return
     */
    public static <T> Result<T> ok() {
        Result<T> result = new Result<>();
        PubInfo pubInfo = new PubInfo(StatusCode.SUCCESS.message(), StatusCode.SUCCESS.code());
        ResultData<T> resultData = new ResultData<>();
        resultData.setPubInfo(pubInfo);
        result.setData(resultData);
        result.setStatus(SUCCESS_CODE);
        return result;
    }

    /**
     * 1.2，成功，返回数据
     *
     * @param data 主要数据
     * @return
     */
    public static <T> Result<T> ok(T data) {
        Result<T> result = new Result<>();
        PubInfo pubInfo = new PubInfo(StatusCode.SUCCESS.message(), StatusCode.SUCCESS.code());
        ResultData<T> resultData = new ResultData<>();
        resultData.setPubInfo(pubInfo);
        resultData.setBusInfo(data);
        result.setData(resultData);
        result.setStatus(SUCCESS_CODE);
        return result;
    }

    /**
     * 1.3，成功，返回分页数据
     *
     * @param page  当前页码
     * @param size  每页显示行数
     * @param total 总记录数
     * @param body  返回主要内容
     * @return
     */
    public static <T> Result<T> successByPage(long page, long size, long total,long num, T body) {
        Result<T> result = new Result<>();
        PubInfo pubInfo = new PubInfo(StatusCode.SUCCESS.message(), StatusCode.SUCCESS.code());
        ResultData<T> resultData = new ResultData<>();
        resultData.setPubInfo(pubInfo);
        resultData.setBusInfo(body);
        PageInfo pageInfo = new PageInfo(page,size,total,num);
        resultData.setPageInfo(pageInfo);
        result.setStatus(SUCCESS_CODE);
        result.setData(resultData);
        return result;
    }


    /**
     * 2.1，失败，返回错误信息错误状态码
     *
     * @param message 错误信息
     * @param code    错误状态码
     * @return
     */
    public static <T> Result<T> error(String message, Integer code) {
        Result<T> result = new Result<>();
        PubInfo pubInfo = new PubInfo(message, code);
        ResultData<T> resultData = new ResultData<>();
        resultData.setPubInfo(pubInfo);
        result.setData(resultData);
        result.setStatus(SUCCESS_CODE);
        return result;
    }

    /**
     * 2.2，失败，返回错误信息错误状态码
     *
     * @param message 错误信息
     * @return
     */
    public static <T> Result<T> error(String message) {
        Result<T> result = new Result<>();
        PubInfo pubInfo = new PubInfo(message, StatusCode.OTHER_ERROR.code());
        ResultData<T> resultData = new ResultData<>();
        resultData.setPubInfo(pubInfo);
        result.setData(resultData);
        result.setStatus(SUCCESS_CODE);
        return result;
    }


}